//
//  TwitterAppDelegate.m
//  TwitterPlane
//
//  Created by kazuyuki takahashi on 10/01/23.
//  Copyright 2010 by invisibledesigner.com. All rights reserved.
//

#import "TwitterAppDelegate.h"
#import "TwitterEngineManager.h"
#import "MenuView.h"
#import "PreferencesController.h"
#import "OAToken_KeychainExtensions.h"

#import "Notifications.h"
#import "NSUserDefaults+App.h"

@implementation TwitterAppDelegate

@synthesize window = window_;


- (void)twitterViewMouseEntered:(NSNotification*)notification
{
    [menuView_ fadein];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(twitterViewMouseEntered:)
                                                 name:TwitterViewMouseEnteredNotification
                                               object:nil];
    
    // 透明でタイトルバーのないウィンドウ
    NSColor *color = [NSColor colorWithDeviceRed:0 green:0 blue:0 alpha:0];
    [window_ setBackgroundColor:color];
    [window_ setStyleMask:0];
    [window_ setLevel:NSFloatingWindowLevel];
    [window_ setOpaque:NO];
    [window_ setAlphaValue:1.0];
    
    // 画面右端に移動して表示
    NSSize screenSize = [[NSScreen mainScreen] frame].size;
    NSRect frame = [window_ frame];
    frame.origin.x = screenSize.width - frame.size.width;
    frame.origin.y = screenSize.height / 2 - frame.size.height / 2;
    [window_ setFrameOrigin:frame.origin];
    [window_ orderFront:nil];
    
    // Spacesの画面スイッチに追従する設定
    NSWindowCollectionBehavior behavior = [window_ collectionBehavior];
    behavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
    [window_ setCollectionBehavior:behavior];
    
    // デフォルト設定
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *defaultDics = [NSMutableDictionary dictionary];
    [defaultDics setObject:[NSNumber numberWithInt:kDisplayNameOptionScreenName] forKey:kUserDefaultsKeyDisplayNameOption];
    [defaultDics setObject:[NSNumber numberWithInt:kDisplayDateOptionRelative] forKey:kUserDefaultsKeyDisplayDateOption];
    [defaults registerDefaults:defaultDics];
    
    // OAuthTokenが保存されていなければ設定画面を表示
    OAToken *token = [[[OAToken alloc] initWithKeychainUsingAppName:@"twitplane" serviceProviderName:@"twitter"] autorelease];
    if (!token) {
        [self openPreferences:nil];
    }
    
    // set up TwitterEngine 
    TwitterEngineManager *manager = [TwitterEngineManager defaultManager];
    [manager setStatusesController:statusesController_];
    [manager getFollowedTimeline];
    [manager checkUserCredentials];
    
    [manager setAutoReloadEnabled:YES];
    
    NSView *contentView = [[self window] contentView];
    NSTrackingArea *ta = [[NSTrackingArea alloc] initWithRect:[contentView bounds]
                                                      options:NSTrackingActiveAlways|NSTrackingMouseEnteredAndExited
                                                        owner:self
                                                     userInfo:nil];
    [contentView addTrackingArea:[ta autorelease]];
}


- (void)mouseEntered:(NSEvent*)event
{
    //do nothing
    //TrackingAreaを仕掛けるために定義が要る
}

- (void)mouseExited:(NSEvent*)event
{
    [menuView_ fadeout];
    [[NSNotificationCenter defaultCenter] postNotificationName:TwitterWindowMouseExitedNotification object:self];
}


- (IBAction)reload:(id)sender
{
    [[TwitterEngineManager defaultManager] getFollowedTimeline];
}

- (IBAction)openPreferences:(id)sender
{
    [[PreferencesController sharedController] showWindow:sender];
}

@end
